package com.haiqiu.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.haiqiu.common.controller.BaseController;
import com.haiqiu.common.page.PageRequest;
import com.haiqiu.common.result.Constants;
import com.haiqiu.common.result.ResultData;
import com.haiqiu.syslog.annotation.NotLog;
import com.haiqiu.system.entity.SysUser;
import com.haiqiu.system.entity.query.UserQuery;
import com.haiqiu.system.entity.vo.UserPwd;
import com.haiqiu.system.entity.vo.SysUserVo;
import com.haiqiu.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

/**
 * @author HaiQiu
 * @date 2021/4/2 23:10
 * @desc
 **/
@Api(tags = "用户管理", description = "用户增删改查等方法")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {


    @Autowired
    private SysUserService sysUserService;


    @NotLog
    @ApiOperation(value = "同步用户在线")
    @RequestMapping(method = RequestMethod.GET, value = "/online")
    public ResultData online() {
        sysUserService.online();
        return decide("同步成功");
    }

    @ApiOperation(value = "强制用户下线")
    @RequestMapping(method = RequestMethod.DELETE, value = "/online")
    public ResultData forceLogout(@ApiParam(value = "用户token") @RequestParam String token) {
        sysUserService.forceLogout(token);
        return decide("下线成功");
    }

    @ApiOperation(value = "用户在线列表")
    @RequestMapping(method = RequestMethod.GET, value = "/online/list")
    public ResultData onlineList(@RequestParam(required = false) String userName,
                                 @RequestParam(required = false) String ipaddr) {
        return decide(sysUserService.onlineList(userName, ipaddr));
    }

    @ApiOperation(value = "新增(用户)")
    @RequestMapping(method = RequestMethod.POST, value = "")
    public ResultData save(@RequestBody @Validated SysUserVo userVo) {
        sysUserService.save(userVo);
        return decide(Constants.SUCCESS_ADD);
    }

    @ApiOperation(value = "个人中心头像上传)")
    @PostMapping(value = "/profile/avatar")
    public ResultData avatarProfile(@RequestParam("avatarfile") MultipartFile file) {
        return decide((Object) sysUserService.avatar(file));
    }

    @ApiOperation(value = "后台列表头像上传)")
    @PostMapping(value = "/avatar")
    public ResultData avatarUpload(@RequestParam("avatarfile") MultipartFile file) {
        return decide((Object) sysUserService.avatarUpload(file));
    }


    @ApiOperation(value = "修改(用户)")
    @RequestMapping(method = RequestMethod.PUT, value = "")
    public ResultData update(@RequestBody @Validated SysUserVo userVo) {
        sysUserService.update(userVo);
        return decide(Constants.SUCCESS_UPDATE);
    }

    @ApiOperation(value = "根据用户ID授权用户角色")
    @RequestMapping(method = RequestMethod.PUT, value = "/authRole")
    public ResultData authRole(@ApiParam("用户ID") @RequestParam Long userId,
                               @ApiParam("角色ID集合") @RequestParam List<Long> roleIds) {
        sysUserService.authRole(userId, roleIds);
        return decide("授权成功");
    }

    @ApiOperation(value = "后台管理重置用户密码")
    @RequestMapping(method = RequestMethod.PUT, value = "/resetPwd")
    public ResultData resetPwd(@RequestBody UserPwd userPwd) {
        sysUserService.resetPwd(userPwd);
        return decide(Constants.SUCCESS_RESET);
    }

    @ApiOperation(value = "用户个人中心修改密码")
    @RequestMapping(method = RequestMethod.PUT, value = "/profile/updatePwd")
    public ResultData updateUserPwd(@RequestBody UserPwd userPwd) {
        sysUserService.updateUserPwd(userPwd);
        return decide(Constants.SUCCESS_UPDATE);
    }

    @ApiOperation(value = "用户个人中心修改信息")
    @RequestMapping(method = RequestMethod.PUT, value = "/profile")
    public ResultData updateUserProfile(@ApiParam("头像") @RequestParam(required = false) String avatar,
                                        @ApiParam("昵称") @RequestParam(required = false) String nickname,
                                        @ApiParam("邮箱") @RequestParam(required = false) String email,
                                        @ApiParam("手机") @RequestParam(required = false) String phone,
                                        @ApiParam("性别") @RequestParam(required = false) Integer sex) {
        sysUserService.updateUserProfile(avatar, nickname, email, phone, sex);
        return decide(Constants.SUCCESS_UPDATE);
    }

    @ApiOperation(value = "激活禁用(用户)")
    @RequestMapping(method = RequestMethod.PUT, value = "/active")
    public ResultData active(@RequestBody SysUser sysUser) {
        return decide(sysUserService.active(sysUser));
    }


    @ApiOperation(value = "批量删除(用户)")
    @RequestMapping(method = RequestMethod.DELETE, value = "")
    public ResultData delete(@RequestBody List<Long> ids) {
        return decide(Constants.DELETE, sysUserService.delete(ids));
    }

    @ApiOperation(value = "获取用户信息")
    @RequestMapping(method = RequestMethod.GET, value = "/info")
    public ResultData info(@RequestParam(required = false) String token) {
        return decide(sysUserService.info(token));
    }


    @ApiOperation(value = "ID获取用户信息")
    @RequestMapping(method = RequestMethod.GET, value = "")
    public ResultData get(@RequestParam Long id) {
        return decide(sysUserService.get(id));
    }


    @ApiOperation(value = "模糊分页(用户)")
    @RequestMapping(method = RequestMethod.POST, value = "/list")
    public ResultData list(@RequestBody PageRequest<UserQuery> request) {
        return decide(sysUserService.list(request));
    }

    @ApiOperation(value = "查询所有(用户)")
    @RequestMapping(method = RequestMethod.POST, value = "/all")
    public ResultData all() {
        return decide(sysUserService.all());
    }

    @ApiOperation(value = "查询所有(用户昵称和用户名和邮箱)")
    @RequestMapping(method = RequestMethod.POST, value = "/emailAll")
    public ResultData emailAll() {
        return decide(sysUserService.emailAll());
    }

    @ApiOperation(value = "导出用户数据")
    @GetMapping(value = "/export")
    public ResultData export() throws IOException {
        sysUserService.export();
        return decide("导出成功");
    }


}
